package com.sgb.kjwl.view.ui.adapter.self;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.kjwl.view.ui.activity.self.ApplyCompanyActivity;
import com.swgk.core.util.LogUtil;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemEcommendedEnterprisesBinding;
import com.sgb.kjwl.model.entity.manage.TheEnterpriseOfEntity;
import com.sgb.kjwl.model.entity.user.CompanyInfoEntity;
import com.sgb.kjwl.view.ui.activity.self.ApplyToJoinTheEnterpriseActivity;
import com.sgb.kjwl.view.ui.activity.self.JoinCompanyActivity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.List;
import java.util.Locale;

/***
 * 企业经营-推荐企业-最新入驻
 */
public class JoinComAdapter extends BaseRecycleAdapter<CompanyInfoEntity> {
    private static final String TAG = "RecommendedEnterprisesAdapter";
    private String search = "";
    private TextView mTvComName;
    private TextView mJoinView;
    private ImageView mAvatar;

    public JoinComAdapter(Context context, List<CompanyInfoEntity> datas) {
        super(context, R.layout.item_join_company, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, final CompanyInfoEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        mTvComName = holder.itemView.findViewById(R.id.tv_com_name);
        mJoinView = holder.itemView.findViewById(R.id.join_view);
        mAvatar = holder.itemView.findViewById(R.id.logo_view);
        RequestOptions options = new RequestOptions();
        options.transform(new CircleCrop());
        if (TextUtils.isEmpty(entity.getCompLogo())) {
            if (entity.getType() == 1) {
                Glide.with(context).load(R.mipmap.shop_img_new_2).apply(options).into(mAvatar);
            } else if (entity.getType() == 2) {
                Glide.with(context).load(R.drawable.icon_team_default).apply(options).into(mAvatar);
            } else {
                Glide.with(context).load(R.drawable.icon_organization_default).apply(options).into(mAvatar);
            }
        } else {
            Glide.with(context).load(entity.getCompLogo()).apply(options).into(mAvatar);
        }
        if (entity.getType() == 1) {
            if (!TextUtils.isEmpty(search) && !TextUtils.isEmpty(entity.getName())) {
                if (entity.getName().toLowerCase().contains(search.toLowerCase())) {
                    SpannableStringBuilder spannable = new SpannableStringBuilder(entity.getName());
                    spannable.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.new_blue)),
                            entity.getName().toLowerCase().indexOf(search.toLowerCase()),
                            entity.getName().toLowerCase().indexOf(search.toLowerCase()) + search.length(),
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    mTvComName.setText(spannable);
                } else {
                    mTvComName.setText("");
                }
            } else {
                mTvComName.setText("");
            }
        } else {
            if (!TextUtils.isEmpty(search) && !TextUtils.isEmpty(entity.getCompName())) {
                if (entity.getCompName().toLowerCase().contains(search.toLowerCase())) {
                    SpannableStringBuilder spannable = new SpannableStringBuilder(entity.getCompName());
                    spannable.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.new_blue)),
                            entity.getCompName().toLowerCase().indexOf(search.toLowerCase()),
                            entity.getCompName().toLowerCase().indexOf(search.toLowerCase()) + search.length(),
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    mTvComName.setText(spannable);
                } else {
                    mTvComName.setText("");
                }
            } else {
                mTvComName.setText("");
            }
        }
        mJoinView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClickJoin(entity);
            }
        });
    }

    public interface JoinTvOnClickListener {
        void onClickJoin(CompanyInfoEntity entity);
    }

    private JoinTvOnClickListener listener;

    public void setListener(JoinTvOnClickListener listener) {
        this.listener = listener;
    }

    public void setBlue(String s) {
        search = s;
    }
}
